<?php


namespace App\GatewayWorker;

use App\Models\Chat;
use App\Tools\JwtTools;
use GatewayWorker\Lib\Gateway;

class GatewayWorker
{
    public static function onWorkerStart($businessWorker)
    {
        echo "onWorkerStart\r\n";
    }

    public static function onConnect($client_id)
    {
        Gateway::sendToClient($client_id, json_encode(['type' => 'onConnect', 'client_id' => $client_id]));
    }

    public static function onWebSocketConnect($client_id, $data)
    {
        //print_r($data);
        // echo "onWebSocketConnect\r\n";
    }

    public static function onMessage($client_id, $message)
    {
        $message = json_decode($message, 256);
        switch ($message['type']) {
            case 'init':
                //发消息人的id
                $uid = intval(JwtTools::transitionId($message['uid']));
                //经济人id
                $brokerId = intval($message['broker_id']);
                //绑定uid和client_id
                Gateway::bindUid($client_id, $uid);
                echo $uid.'========'.$brokerId;
                $chatMessage = Chat::whereIn('uid', [$uid,$brokerId])->whereIn('tid', [$uid,$brokerId])->get()->toArray();
                print_r($chatMessage);
                //如果又历史聊天记录
                if ($chatMessage) {
                    foreach ($chatMessage as $value) {
                        $speaker = 'server';
                        //判断消息类型是否是我自己发的
                        if ($value['uid'] != $uid) {
                            $historyMessage['content'] = $value['content'];
                            $historyMessage['speaker'] = $speaker;
                            $historyMessage['type'] = 'client_send';
                            print_r($historyMessage);
                            Gateway::sendToUid($uid, json_encode($historyMessage, 256));
                        } else {
                            $historyMessage['type'] = 'client_send';
                            $historyMessage['speaker'] = '';
                            $historyMessage['content'] = $value['content'];
                            print_r($historyMessage);
                            Gateway::sendToUid($uid, json_encode($historyMessage, 256));
                        }
                    }
                }
                break;
            case 'client_send':
                //发消息人的id
                $uid = JwtTools::transitionId($message['uid']);
                //经济人id
                $brokerId = $message['broker_id'];
                //存入mongodb
                Chat::create([
                    'uid' => $uid,
                    'tid' => intval($brokerId),
                    'content' => $message['content'],
                    'status' => 0
                ]);
                Gateway::sendToUid($brokerId, json_encode($message, 256));
                break;
            case 'broker_send':
                $message['speaker'] = 'server';
                $userId = $message['client_id'];
                Gateway::sendToUid($userId, json_encode($message, 256));
                break;
            //绑定场景值为uid
            case 'bind':
                $eventKey = $message['data']['eventKey'];
                Gateway::bindUid($client_id, $eventKey);
                break;
            //发送openid和场景值给登陆页面
            case 'send_message':
                $eventKey = $message['data']['eventKey'];
                Gateway::sendToUid($eventKey, json_encode($message, 256));
                break;
        }


    }

    public static function onClose($client_id)
    {
        echo "onClose\r\n";
    }
}
